#define _CRT_SECURE_NO_WARNINGS
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        int n = 0; vector<vector<int>> ret;
        vector<int> ins;
        queue<TreeNode*> ser;
        if (root)
        {
            n = 1;
            ser.push(root);
        }
        while (!ser.empty())
        {
            while (n--)
            {
                struct TreeNode* cur = ser.front();
                ins.push_back(cur->val);
                if (cur->left)
                    ser.push(cur->left);
                if (cur->right)
                    ser.push(cur->right);
                ser.pop();
            }
            n = ser.size(); ret.push_back(ins);
            ins.clear();
        }
        reverse(ret.begin(), ret.end());
        return ret;
    }

};